const http = require("http");
const fs = require("fs");

const server = http.createServer((request, response) => {
  //获取请求方法
  let { method } = request;
  //获取请求的url路径
  let { pathname } = new URL(request.url, "http://127.0.0.1:9000");
  response.setHeader("content-type", "text/html;charset=utf-8");

  response.write("我是响应体1");
  response.write("我是响应体2");

  if (method === "GET" && pathname === "/login") {
    response.end("登录页面");
  } else if (method === "GET" && pathname === "/reg") {
    response.end("注册页面");
  } else {
    if (pathname === "/") {
      let html = fs.readFileSync(__dirname + "/login.html");
      response.end(html);
    } else if (pathname === "/login.css") {
      let css = fs.readFileSync(__dirname + "/login.css");
      response.end(css);
    } else if (pathname === "/login.js") {
      let js = fs.readFileSync(__dirname + "/login.js");
      response.end(js);
    } else {
      response.statusCode = 404;
      response.end("404 not found");
    }
  }
});

//http://127.0.0.1:9000/ , 终端本地 node guide.js
server.listen(9000, () => {
  console.log("服务已启动");
});
